import java.util.Locale;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-05-28
 * Time:14:37
 */
public class Test {
    public static void main(String[] args) {
        String s1="abc"+"def";//1
        String s2=new String(s1);//2
        if(s1.equals(s2))//3
            System.out.println(".equals succeeded");//4
        if(s1==s2)//5
            System.out.println("==succeeded");//6
    }


    public static void main10(String[] args) {
        String str = "    abcd   abcd   abcd    ";
        System.out.println(str.trim());//去掉字符串中的左右空格,保留中间空格
    }

    public static void main9(String[] args) {
        //字符串截取
        String str = "abcdef";
        String ret = str.substring(2);
        System.out.println("ret:"+ret);
        String ret2 = str.substring(2,4);//[2,4)
        System.out.println(ret2);
    }

    public static void main8(String[] args) {
        String str = "name=zhangfei&age=18";
        String[] ret = str.split("&");
        for (String x:ret) {
            String[] ss = x.split("=");
            for (String s:ss) {
                System.out.println(s);
            }
        }
    }

    public static void main7(String[] args) {
        //字符串拆分
        String s1 = "hahaha hahaha happy new year";

        //以空格来拆分
        String[] ret = s1.split(" ");
        for (String s:ret) {
            System.out.println(s);
        }
        System.out.println("-------------------");
        String[] ret2 = s1.split(" ",4);//limit:控制分多少组！！
        for (String s:ret2) {
            System.out.println(s);
        }
        System.out.println("-------------------");
        //特殊字符需要转义："."
        String s2 = "192.192.13.15.16";
        String[] ret3 = s2.split("\\.");//"\\."->".";即\为转义字符
        for (String s:ret3) {
            System.out.println(s);
        }
        System.out.println("---------------------------------");
        //特殊的字符:\
        String s3 = "192\\192\\13\\15\\16";
        String[] ret4 = s3.split("\\\\");
        for (String s:ret4) {
            System.out.println(s);
        }
        System.out.println("---------------------------------");
        //有多个字符进行分割
        String str = "hehehe ehhe&ndn";
        String[] count = str.split("[ &]");//如果一个字符串中有多个分隔符，可以用"|"作为连字符
        for (String s:count) {
            System.out.println(s);
        }
    }

    public static void main6(String[] args) {
        //字符串替换
        String s1 = "ababababcabcdacbbab";
        System.out.println(s1.replace('a', 'p'));
        System.out.println(s1);
        System.out.println("------------------------------------------");
        System.out.println(s1.replace("ab", "hh"));
        System.out.println("------------------------------------------");
        System.out.println(s1.replaceFirst("ab", "hh"));

    }

    public static void main5(String[] args) {
        //格式化
        String s = String.format("%d-%d-%d", 2022, 5,28);
        System.out.println(s);
    }


    public static void main4(String[] args) {
        //字符串转数组
        String s1 = "hello";
        char[] chars = s1.toCharArray();
        for (char x:chars) {
            System.out.print(x+" ");
        }
        System.out.println(s1);
    }

    public static void mai3(String[] args) {
        //大小写转换
        String s1 = "hello阿文";//小写转大写，转换于中文无关！
        String ret = s1.toUpperCase(Locale.ROOT);
        System.out.println("new:"+ret);
        System.out.println("原本s1:"+s1);
        System.out.println("------------------------------------");
        String s2 = "HELLO阿文";
        String ret2 = s2.toLowerCase(Locale.ROOT);
        System.out.println("new:"+ret2);
        System.out.println("原本s2:"+s2);
        //改变大小不是说将原本的字符串本身给改变了，而是产生了一个新的对象！
    }


    public static void main2(String[] args) {
        //数字转字符串
        String s = String.valueOf(123);
        String s2 = String.valueOf(12.3);
        String s3 = String.valueOf(12.3f);
        System.out.println(s);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println("-----------------------------------------------");
        //字符串转数字
        int a = Integer.valueOf("12");//默认十进制的
        System.out.println(a);
        int a2 = Integer.valueOf("64",8);//8进制转化64:6*8^1+4*8^0 = 52
        System.out.println(a2);
        //parseInt于valuof是一样的
        int a3 = Integer.parseInt("12");//默认十进制的
        System.out.println(a);
        int a4 = Integer.parseInt("64",8);//8进制转化64:6*8^1+4*8^0 = 52
        System.out.println(a2);
    }


    public static void main1(String[] args) {
        String s1 ="hello";
        String s2 = "abcdababababcabab";
        char ch = s1.charAt(0);//char charAt(int index),返回index位置上字符，
        System.out.println(ch);
        System.out.println("===================================================");
        int ret = s1.indexOf('l');//查找第一次出现的字符/字符串
        int ret2 = s2.indexOf("abc");
        System.out.println("ret:"+ret);
        System.out.println("ret2:"+ret2);
        System.out.println("===================================================");
        int index = s1.indexOf('l',3);
        int index2 = s1.indexOf("abc",3);
        System.out.println("index:"+index);
        System.out.println("index2:"+index2);
        System.out.println("===================================================");
        int count = s1.lastIndexOf('o');
        System.out.println(count);
        int count2 = s2.lastIndexOf("abc",3);
        System.out.println(count2);
        System.out.println("===================================================");

    }
}
